package com.vcit.StudentGrade.controller;

import com.vcit.common.annotation.Log;
import com.vcit.common.core.controller.BaseController;
import com.vcit.common.core.domain.AjaxResult;
import com.vcit.common.enums.BusinessType;
import com.vcit.system.domain.vo.GraduationGradeVo;
import com.vcit.system.service.IGraduationGradeService;
import com.vcit.system.util.GraduationGradeExcelUtil;
import com.vcit.system.util.ExcelTemplateGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 毕业成绩单导出Controller
 * 
 * @author vcit
 */
@Api("毕业成绩单导出")
@RestController
@RequestMapping("/system/graduationGrade")
public class GraduationGradeController extends BaseController {
    
    @Autowired
    private IGraduationGradeService graduationGradeService;
    
    @Autowired
    private GraduationGradeExcelUtil graduationGradeExcelUtil;
    
    @Autowired
    private ExcelTemplateGenerator excelTemplateGenerator;
    
    /**
     * 导出学生毕业成绩单
     */
    @PreAuthorize("@ss.hasPermi('system:graduationGrade:export')")
    @GetMapping("/export")
    @ApiOperation("导出学生毕业成绩单")
    @Log(title = "毕业成绩单导出", businessType = BusinessType.EXPORT)
    public void exportGraduationGrade(@RequestParam String studentNo, HttpServletResponse response) {
        try {
            // 1. 获取毕业成绩单数据
            GraduationGradeVo graduationGrade = graduationGradeService.getGraduationGradeByStudentNo(studentNo);
            // 2. 导出Excel
            graduationGradeExcelUtil.exportGraduationGrade(response, graduationGrade, graduationGrade.getName());
        } catch (Exception e) {
            logger.error("导出毕业成绩单失败", e);
            // 不再写response内容，避免 getOutputStream() has already been called for this response
        }
    }
    
    /**
     * 获取学生毕业成绩单数据（用于预览）
     */
   // @PreAuthorize("@ss.hasPermi('system:graduationGrade:query')")
    @GetMapping("/data")
    @ApiOperation("获取学生毕业成绩单数据")
    public AjaxResult getGraduationGradeData(@RequestParam String studentNo) {
        try {
            GraduationGradeVo graduationGrade = graduationGradeService.getGraduationGradeByStudentNo(studentNo);
            return success(graduationGrade);
        } catch (Exception e) {
            logger.error("获取毕业成绩单数据失败", e);
            return error("获取数据失败：" + e.getMessage());
        }
    }
    
    /**
     * 生成Excel模板
     */
    @PreAuthorize("@ss.hasPermi('system:graduationGrade:export')")
    @GetMapping("/generateTemplate")
    @ApiOperation("生成Excel模板")
    public AjaxResult generateTemplate() {
        try {
            String templatePath = "export_grades_template.xlsx";
            excelTemplateGenerator.generateGraduationGradeTemplate(templatePath);
            return success("模板生成成功，文件路径：" + templatePath);
        } catch (Exception e) {
            logger.error("生成模板失败", e);
            return error("生成模板失败：" + e.getMessage());
        }
    }
} 